package com.three.controller.old; /**
 * @author wzm
 * @create 2023-11-07 16:53
 * project_name:web2310
 * @Description
 */

import com.three.pojo.User;
import com.three.service.UserService;
import com.three.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

//匹配访问路径
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    //上调下 私有化
    private UserService userService = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //实现登录功能

        //0.设置编码方式
        request.setCharacterEncoding("UTF-8");//请求
        response.setContentType("text/html;charset=UTF-8");//响应

        //1.接收前端参数用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.调用业务逻辑层servcie实现登录的操作
        User login = userService.login(new User(null, username, password));


        //3.根据结果进行判断

        PrintWriter writer = response.getWriter();
        if (login != null) {
            //登录成功
            //4.响应结果到浏览器
            writer.write("登录成功");
        } else {
            //登录失败
            writer.write("登录失败");
        }


    }

}
